import 'package:flutter/material.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/ui/my/order/recover/offline_order_viewmodel.dart';
import 'package:gold/ui/my/order/recover/recover_config.dart';
import 'package:gold/utils/res.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:gold/base/provider_widget.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description: 黄金回收-线下订单列表页面
/// @author tangzhentao
/// @created at 21:32 2024/12/14

class OfflineOrderPage extends StatefulWidget {

  const OfflineOrderPage({super.key});

  @override
  State<StatefulWidget> createState() => _OfflineOrderListState();
}

class _OfflineOrderListState extends State<OfflineOrderPage> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      margin: EdgeInsets.only(top: XfgSize().size10),
      color: XfgColor.background,
      child: ProviderWidget<OfflineOrderViewmodel>(
          model: OfflineOrderViewmodel(ApiGold.recycleList),
          onInit: (data) => data.firstLoad(),
          builder: (ctx, order) => SmartRefresher(
            controller: order.controller,
            enablePullUp: true,
            enablePullDown: true,
            onRefresh: () => order.refresh(),
            onLoading: () => order.loadMore(),
            child: ListView.builder(
                itemCount: order.list.length,
                itemBuilder: (_, index) {
                  final model = order.list[index];
                  final isLast = index == order.list.length - 1;
                  return GestureDetector(
                    onTap: () => order.toDetailPage(index),
                    child: Container(
                      decoration: white10Decoration,
                      margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, isLast ? XfgSize().bottomBarHeight:0),
                      child: Column(
                        children: [
                          Container(
                            height: XfgSize().size49,
                            padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                            child: Row(
                              children: [
                                XfgText('黄金料 ${model.weight?? '--'}g', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold),
                                const Spacer(),
                                Image.asset(Res.imagesArrowRight, width: XfgSize().size10, height: XfgSize().size15)
                              ],
                            ),
                          ),
                          strokeHeightContainer,
                          Padding(
                            padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size18, 0, XfgSize().size18),
                            child: Column(
                              children: [
                                /// 金价，预估总价
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Expanded(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.end,
                                          children: [
                                            XfgText('金价', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold),
                                            SizedBox(width: XfgSize().size8),
                                            XfgText('¥', fontSize: XfgSize().text12, color: XfgColor.text1D, bold: FontWeight.bold),
                                            ...getRecoverMoneyWidgets(model.price, XfgColor.text1D)
                                          ],
                                        )
                                    ),
                                    Expanded(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.end,
                                          children: [
                                            XfgText('预估总价', fontSize: XfgSize().text14, color: XfgColor.theme, bold: FontWeight.bold),
                                            SizedBox(width: XfgSize().size8),
                                            XfgText('¥', fontSize: XfgSize().text12, color: XfgColor.theme, bold: FontWeight.bold),
                                            ...getRecoverMoneyWidgets(model.totalPrice, XfgColor.theme)
                                          ],
                                        )
                                    ),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: XfgSize().size18),
                                  alignment: Alignment.centerLeft,
                                  child: XfgText('预约时间：${model.arriveTime?? '--'}', fontSize: XfgSize().text14, color: XfgColor.text2),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                }
            ),
          )
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
